package com.fis.test3.service.impl;

import com.fis.test3.Repository.BDataRepository;
import com.fis.test3.Repository.FPRepository;
import com.fis.test3.Repository.FTRepository;
import com.fis.test3.Repository.RTimeProRepository;
import com.fis.test3.model.BindingData;
import com.fis.test3.model.FiberPerpare;
import com.fis.test3.model.FiberTypes;
import com.fis.test3.model.RealTimeProcess;
import com.fis.test3.service.FiberPerpareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@Service
public class FiberPerpareServiceImpl implements FiberPerpareService {

    @Autowired
    private FPRepository fpRepository;
    @Autowired
    private FTRepository ftRepository;
    @Autowired
    private BDataRepository bdRepository;

    @Autowired
    private RTimeProRepository rtpRepository;

    @Override
    public FiberPerpare FiberPerpareSave(FiberPerpare fp) {

        if (fpRepository.existsByContractId(fp.getContractId())){

            //更新FiberPerpare
            if (ftRepository.existsByContractIdAndFibTypeNameAndFiberLength(fp.getContractId(),
                    fp.getFiberType(),fp.getFiberLength())){
                FiberPerpare fiberPerpare = fpRepository.getByContractId(fp.getContractId());
                fp.setId(fiberPerpare.getId());
                fpRepository.save(fp);
            }
            // 插入新的ftRepository
            else {
                FiberTypes ft =new FiberTypes();
                ft.setContractId(fp.getContractId());
                ft.setFibTypeName(fp.getFiberType());
                ft.setFiberLength(fp.getFiberLength());
                ftRepository.save(ft);
            }
        }
        //插入新的FiberPerpare ，插入新的ftRepository
        else {
            fpRepository.save(fp);
            FiberTypes ft =new FiberTypes();
            ft.setContractId(fp.getContractId());
            ft.setFibTypeName(fp.getFiberType());
            ft.setFiberLength(fp.getFiberLength());
            ftRepository.save(ft);
            //生成产品编号并插入表BindingData
            List<BindingData> listBD = new ArrayList<>() ;
            for (int i=1 ;i<=fp.getFiberNum();i++){
                BindingData bd =new BindingData();
                bd.setProductNum(fp.getContractId()+"-"+i);
                listBD.add(bd);
            }
            if (!listBD.isEmpty()){
               bdRepository.saveAll(listBD);
            }
        }
        return fp;
    }

    @Override
    public Collection<BindingData> getProductNumList(String contractId) {
        List<BindingData> bindingDatas =bdRepository.findProductNameLike("%"+contractId+"%");

        return bindingDatas;
    }

    @Override
    public Collection<FiberTypes> getfibTypeNameLength(String contractId) {
        return ftRepository.findFibTypeNameLengthLike(contractId);
    }


    public BindingData BindingDataSave(BindingData bd) {
        //如果已有IC,就更新
//        if(bdRepository.existsByICname(bd.getiCname())){
//            BindingData bddata =  bdRepository.getByICname(bd.getiCname());
//            bd.setId(bddata.getId());
//            bdRepository.save(bd);
//        }
//
//        else {
        //如果没有IC,就插入新的
           //直接在ProductNum一行更新
         BindingData bddata =  bdRepository.getByProductNum(bd.getProductNum());
            bd.setId(bddata.getId());
            bdRepository.save(bd);
//        }
        return bd;
    }

    @Override
    public RealTimeProcess RealTimeProcessSave(RealTimeProcess rtp) {
        if ("穿散件"==rtp.getWorkProcess()){
            if (rtpRepository.existsByICnameAndWorkProcess(rtp.getiCname(),rtp.getWorkProcess())){

            }
            else
                { rtp.setProcessNum(1);
                rtp.setProcessGroup("process1");
                rtp.setCreateTime(new Date());
                rtpRepository.save(rtp);
                }

        }


        return rtp;
    }
}
